/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

public abstract class AddItemsToPackageContentPanel extends Panel {

    Component parent;
//    String packageId = "";
    ListView listView;
    MyItemPackage itemPackage;
    WebMarkupContainer theContainer;
    DefaultDataTable dataTable;
    HashMap<String, Boolean> packageContent = new HashMap<String, Boolean>();

    public AddItemsToPackageContentPanel(String id, Component parent, MyItemPackage itemPackage) {
        super(id);
        this.parent = parent;

        this.theContainer = new WebMarkupContainer("theContainer");
        this.theContainer.setOutputMarkupId(true);
        this.itemPackage = itemPackage;
        List<IColumn> columns = new ArrayList<IColumn>();

//         this.packageId = packageID;
//
//         System.out.println("________________PACKAGEID: " + this.packageId);

        columns.add(new AbstractColumn(new Model("Contains")) {

            public void populateItem(Item cellItem, String componentId, IModel model) {
                System.out.println("model: " + ((MyItem) ((DetacheableItemsModel) model).load()).getId());
                cellItem.add(new PackageItemCheckBoxPanel(componentId, AddItemsToPackageContentPanel.this.itemPackage, ((MyItem) ((DetacheableItemsModel) model).load()).getId()) {

                    @Override
                    public void checkBoxValueChanged() {
                        AddItemsToPackageContentPanel.this.valueChanged(this.itemPackage, this.itemId);
                    }
                });
            }
        });

        columns.add(new PropertyColumn(new Model("ID"), "id") {
//            @Override
//            public String getCssClass()
//            {
//                return "numeric";
//            }
        });

        columns.add(new PropertyColumn(new Model("Category"), "category") {
//            @Override
//            public String getCssClass()
//            {
//                return "numeric";
//            }
        });

        columns.add(new PropertyColumn(new Model("Name"), "name"));

//        columns.add(new PropertyColumn(new Model("Last Name"), "lastName", "lastName")
//        {
//            @Override
//            public String getCssClass()
//            {
//                return "last-name";
//            }
//        });

        columns.add(new PropertyColumn(new Model("Weight"), "weight"));
        columns.add(new PropertyColumn(new Model("Unit"), "unit"));
        columns.add(new PropertyColumn(new Model("Count"), "count"));
        columns.add(new PropertyColumn(new Model("Price"), "price"));

//        columns.add(new AbstractColumn(new Model("Actions2")) {
//
//            public void populateItem(Item cellItem, String componentId, IModel model) {
//                cellItem.add(new InDecreasePanel(componentId, model, MyItems.this));
//            }
//        });

        this.dataTable = new DefaultDataTable("table", columns, new SortableItemsProvider(), 10);
        this.theContainer.add(this.dataTable);


        this.add(this.theContainer);
//        this.itemPackageForm = new ItemPackageForm("itemPackageForm", parent);
//        add(itemPackageForm);
////        this.itemsPanel = new PackageItemsPanel("packageContent");
////        this.itemsPanel.setVisible(true);
////        add(this.itemsPanel);
//
//        this.theContainer.setOutputMarkupId(true);
//        this.listView = new ListView("allItems") {
//
//            @Override
//            protected void populateItem(ListItem arg0) {
//                // nothing yet
//            }
//        };
//        this.theContainer.add(this.listView);
//        this.add(theContainer);
////        add(this.listView);
////        this.add(this.listView);
////        this.addListView();
//
//        // Create the form, to use later for the buttons
        add(new Label("textLabel", ""));
        add(new AjaxLink("confirm") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                AddItemsToPackageContentPanel.this.savePackageContent();
                target.addComponent(AddItemsToPackageContentPanel.this.theContainer);
                onSelect(target, "CONFIRM");
                System.out.println("PARENT: " + AddItemsToPackageContentPanel.this.parent.getClassRelativePath());
                System.out.println("CLICK");

//                target.addComponent(AddItemsToPackageContentPanel.this.parent);
            }
        });
    }

    public void populateItem(ListItem item) {
    }

    private void addListView() {

//        this.theContainer.remove("allItems");
//
//        this.itemPackage = MyItemPackageManager.getInstance().getItemPackage(AddItemsToPackageContentPanel.this.itemId);
//        List<MyItem> itemList = itemPackage.getItemList();
//
//        this.listView = new ListView("allItems", itemList) {
//
//            protected void populateItem(ListItem item) {
//
//                MyItem actItem = (MyItem) item.getModelObject();
//
//                item.add(new InDecreasePackageItemPanel("indecrease", AddItemsToPackageContentPanel.this.itemId, item.getModel(), AddItemsToPackageContentPanel.this.theContainer));
//                item.add(new Label("id", actItem.getId()));
//                item.add(new Label("category", actItem.getCategory()));
//                item.add(new Label("name", actItem.getName()));
//                item.add(new Label("weight", "" + actItem.getWeight()));
//                item.add(new Label("unit", "" + actItem.getUnit()));
//                item.add(new Label("count", "" + AddItemsToPackageContentPanel.this.itemPackage.getItems().get(actItem.getId()).intValue()));
//                item.add(new Label("price", "" + actItem.getPrice()));
//            }
//        };
//
//        this.listView.setOutputMarkupId(true);
//        this.theContainer.add(this.listView);
//        add(this.listView);
//        add(pContent);
    }

    public void setItemToEdit(String itemId) {
//        this.itemId = itemId;
//        itemPackageForm.setItemPackage(MyItemPackageManager.getInstance().getItemPackage(itemId));
//        itemPackageForm.setModelObject(MyItemPackageManager.getInstance().getItemPackage(itemId));
//
////        this.itemsPanel.setItemToEdit(itemId);
//        this.addListView();
    }

    public void setMessageText(String text) {
        this.remove("textLabel");
        add(new Label("textLabel", text));
    }

    public void setPackage(MyItemPackage itemPackage) {
        this.itemPackage = itemPackage;
    }

    private void valueChanged(MyItemPackage itemPackage, String itemId) {

        if (!this.packageContent.containsKey(itemId)) {
            if (itemPackage.containsItem(itemId)) {
                this.packageContent.put(itemId, Boolean.FALSE);
            } else {
                this.packageContent.put(itemId, Boolean.TRUE);
            }
        } else {
            Boolean newB = this.packageContent.get(itemId).booleanValue() ? Boolean.FALSE : Boolean.TRUE;
            this.packageContent.put(itemId, newB);
        }

    }

    private void savePackageContent() {

        Iterator<String> keyIt = this.packageContent.keySet().iterator();
        String actKey;
        while (keyIt.hasNext()) {
            actKey = keyIt.next();
            if (this.packageContent.get(actKey).booleanValue()) {
                if (!itemPackage.containsItem(actKey)) {
                    itemPackage.addItem(actKey, 1);
                }
            } else {
                if (itemPackage.containsItem(actKey)) {
                    itemPackage.removeItem(actKey);
                }
            }


            System.out.println("=> " + actKey + "= " + this.packageContent.get(actKey).toString());
        }
        
        List<MyItem> l = itemPackage.getItemList();
        System.out.println("Package-ITEMS in CONTENTPANEL");

        for (int i = 0; i < l.size(); i++) {
            System.out.println("I: " + l.get(i) + "- " + itemPackage.getItems().get(l.get(i).getId()));
        }
    }

    public MyItemPackage getEditedPackage() {
        return this.itemPackage;
    }

    abstract void onCancel(AjaxRequestTarget target);

    abstract void onSelect(AjaxRequestTarget target, String selection);
}

